package com.tc.activiti.listener;

import com.tc.activiti.util.AppUtil;
import com.tc.biz.entity.SysUser;
import com.tc.biz.service.IUserService;
import com.tc.biz.service.impl.UserServiceImpl;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.Process;
import org.activiti.bpmn.model.UserTask;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.impl.RepositoryServiceImpl;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.PvmActivity;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *
 * @author zengqingfa
 * @date 2019/10/30 15:20
 * @description
 * @email zengqingfa_java@163.com
 * </p>
 * 管养单位人监听器
 */
@Component
public class PipeDepartmentPersonTaskListener implements TaskListener {

    //管养单位组织id   设置为1
    public static final String PIPE_DEPARTMENT_ID_ = "8";

    @Override
    public void notify(DelegateTask delegateTask) {
        IUserService userService = AppUtil.getBean(UserServiceImpl.class);
        //根据部门id查询该部门人员
        List<SysUser> userList = userService.findUserByDeptId(PIPE_DEPARTMENT_ID_);
        //也可以设置变量,需要在设定办理人的时候使用ul表达式  ${assigne}
        // delegateTask.setVariable("assigne", "3");
//        if (CollUtil.isNotEmpty(userList)) {
//            for (SysUser sysUser : userList) {
//                delegateTask.addCandidateUser(sysUser.getUserId().toString());
//            }
//        }
//        getNextPerson(delegateTask);
    }


    /**
     * 获取当前节点中下一节点的审批人
     *
     * @param delegateTask
     */
    public void getNextPerson(DelegateTask delegateTask) {
        RuntimeService runtimeService = AppUtil.getBean(RuntimeService.class);
        RepositoryService repositoryService = AppUtil.getBean(RepositoryService.class);
        ProcessDefinitionEntity def = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService).getDeployedProcessDefinition(delegateTask.getProcessDefinitionId());
        List<ActivityImpl> activitiList = def.getActivities();  //rs是指RepositoryService的实例
        ExecutionEntity execution = (ExecutionEntity) runtimeService.createExecutionQuery().executionId(delegateTask.getExecutionId()).singleResult();
        String activitiId = execution.getActivityId();
        for (ActivityImpl activityImpl : activitiList) {
            String id = activityImpl.getId();
            if (activitiId.equals(id)) {
                System.out.println("当前任务：" + activityImpl.getProperty("name")); //输出某个节点的某种属性
                List<PvmTransition> outTransitions = activityImpl.getOutgoingTransitions();//获取从某个节点出来的所有线路
                for (PvmTransition tr : outTransitions) {
                    PvmActivity ac = tr.getDestination(); //获取线路的终点节点
                    String s = (String) ac.getProperty("multiInstance");
//                    if ("parallel".equals(s)) {
                    String targetUserTaskId = ac.getId();
                    BpmnModel bpmnModel = repositoryService.getBpmnModel(def.getId());
                    List<Process> processes = bpmnModel.getProcesses();
                    for (Process process : processes) {
                        List<UserTask> userTaskList = process.findFlowElementsOfType(UserTask.class);
                        for (UserTask userTask : userTaskList) {
                            if (userTask.getId().equals(targetUserTaskId)) {
                                List<String> candidateUsers = userTask.getCandidateUsers();
                           /*String[] assingneeArray=complateDto.getUserIds().split(",");
                           variables.put("assigneeList", Arrays.asList(assingneeArray));*/
                                String assignee = userTask.getAssignee();
                                System.out.println(candidateUsers);
                                Map<String, String> map = new HashMap<>();
                                map.put("zhenggaifuhe", assignee);
                                //设置到全局变量中去
                                runtimeService.setVariables(delegateTask.getExecutionId(), map);
                            }
                        }
                    }
//                    }
                }
            }
        }

    }
}
